package com.ticketbooking.service.impl;

import com.ticketbooking.entity.OrderInfEntity;
import com.ticketbooking.service.IOrderInfDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderInfDaoImpl implements IOrderInfDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List getOrderInfByOrderId(String orderid) {
        String sql = "select b.airline,b.airtype,b.departure,b.destination,b.depairport,b.desairport,a.routeid,a.deptime,a.fare,a.orderid,a.seatnum,a.state,a.userid from orderinf a inner join routeinf b on a.routeid =b.routeid and a.deptime = b.deptime where a.orderid = ?";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{orderid},new int[]{Types.VARCHAR});
        return list;
    }
    @Override
    public List getOrderInfByUserId(String userid){
        String sql = "select b.airline,b.airtype,b.departure,b.destination,b.depairport,b.desairport,a.routeid,a.deptime,a.fare,a.orderid,a.seatnum,a.state,a.userid from orderinf a inner join routeinf b on a.routeid =b.routeid and a.deptime = b.deptime where a.userid = ?";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{userid},new int[]{Types.VARCHAR});
        return list;
    }
    @Override
    public List getOrderInfnum(){
        String sql = "select realname,a.userid,usercard,userphone,count(a.userid) usernum from orderinf a inner join userinf b on a.userid =b.userid group by realname,usercard,userphone";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }



    @Override
    public void save(OrderInfEntity orderinf) {
        String sql = "insert into orderinf(Orderid,Routeid,Seatnum,Fare,booktime,state,deptime) values (?,?,?,?,?,?,?)";
        Object args[] = {orderinf.getOrderid(),orderinf.getRouteid(),orderinf.getSeatnum(),orderinf.getFare(),orderinf.getBooktime(),orderinf.getState(),orderinf.getDeptime()};
        int[] argTypes = {Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.TIMESTAMP,Types.VARCHAR,Types.TIMESTAMP};
        //DATETIME的JDBC索引类型是timestamp.
        jdbcTemplate.update(sql,args,argTypes);
    }

    @Override
    public List getAllOrderInf()
    {
        String sql = "select b.airline,b.airtype,b.departure,b.destination,b.depairport,b.desairport,a.routeid,a.deptime,a.fare,a.orderid,a.seatnum,a.state,a.userid from orderinf a inner join routeinf b on a.routeid =b.routeid and a.deptime = b.deptime ";
        List<Map<String, Object>> orderinflist = jdbcTemplate.queryForList(sql);
        return orderinflist;
    }
    private OrderInfEntity getOrderInfEntity(List<Map<String, Object>> orderInfList ){
        OrderInfEntity orderinf = new OrderInfEntity();
        if(orderInfList.size()>0){
            Map<String, Object> map = orderInfList.get(0);
            orderinf.setFare((String)map.get("Fare"));
            orderinf.setUserid((String)map.get("userid"));
            orderinf.setOrderid((String)map.get("Orderid"));
            orderinf.setRouteid((String)map.get("Routeid"));
            orderinf.setSeatnum((String)map.get("Seatnum"));
            orderinf.setBooktime((String)map.get("booktime"));
            orderinf.setState((String)map.get("State"));
            orderinf.setDeptime((String)map.get("Depetime"));
        }
        return orderinf;
    }

}
